package com.fp.voiceshoper.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fp.fpShoper.R;
import com.fp.voiceshoper.db.DbOpenHelper;
import com.fp.voiceshoper.interfaces.ListItemsListener;


public class ShopListsAdapter extends ArrayAdapter<String> {

	private List<String> list;
	private Context context;
	private ListItemsListener mListItemsListener;
	
	private DbOpenHelper db;
	
	public ShopListsAdapter(Context context, List<String> list, ListItemsListener listlistener) {
		super(context, R.layout.saveditem, list);
		this.context=context;
		this.list = list;
		this.mListItemsListener=listlistener;
		this.db=new DbOpenHelper(context);
	}
	
	@Override
	public int getCount() {
		
		
		// TODO Auto-generated method stub
		return list.size();
	}
	
	public Boolean contain(String name) 
	{
		return list.contains(name);
	}

	@Override
	public String getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
		
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public int getItemByName(String val){
		return list.indexOf(val);
	}
	
	public void addItem(String item){
		list.add(item);
		notifyDataSetChanged();
	}
	
	public String getString(int id){
		return list.get(id);
	}

	
	@Override
	public View getDropDownView(int position, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		return getCustomView(position, convertView, parent,true);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return getCustomView(position, convertView, parent,false);
	}

	public View getCustomView(int position, View convertView,
			ViewGroup parent,Boolean wl) {
		// TODO Auto-generated method stub
		// return super.getView(position, convertView, parent);

		View rowView;
	    if (wl){
	    	final int pos =position;
			LayoutInflater inflater = (LayoutInflater) context
	                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	      
		
			rowView = inflater.inflate(R.layout.drop_down_list_row, parent, false);
	
	        TextView textView = (TextView) rowView.findViewById(R.id.label_list);
	        
	      
	    
	        	ImageView imageView = (ImageView) rowView.findViewById(R.id.delete_list);
//	        	  textView.setOnClickListener(new OnClickListener() {
//	  				
//	  				@Override
//	  				public void onClick(View v) {
//	  					// TODO Auto-generated method stub
//	  					mListItemsListener.onDrop(2);
//	  				}
//	  			});
//	  	             
	  	        imageView.setOnClickListener(new OnClickListener() {
	  				
	  				@Override
	  				public void onClick(View arg0) {
	  					// TODO Auto-generated method stub
	  					mListItemsListener.onDrop(1,pos);
	  					
	  				}
	  			});
	  	        
	  	        if (db.haveAlarm(list.get(position))){
	  	        	ImageView imageViewAlarm = (ImageView) rowView.findViewById(R.id.alarm_image);
	  	        	imageViewAlarm.setVisibility(View.VISIBLE);
	  	        	imageViewAlarm.setOnClickListener(new OnClickListener() {
	 	  				
	 	  				@Override
	 	  				public void onClick(View arg0) {
	 	  					// TODO Auto-generated method stub
	 	  					mListItemsListener.onDrop(2,pos);
	 	  					
	 	  				}
	 	  			});
	  	        }
	  	        
	  		    textView.setText(list.get(position));
	  		   
	        }
	        else{
	        	LayoutInflater inflater = (LayoutInflater) context
		                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	      
		
				rowView = inflater.inflate(R.layout.saveditem, parent, false);
		
		        TextView textView = (TextView) rowView.findViewById(R.id.saveditem);

	  		    textView.setText(list.get(position));
	  		    
	  		    
	  		  if (db.haveAlarm(list.get(position))){
	  	        	ImageView imageViewAlarm = (ImageView) rowView.findViewById(R.id.alarm_image);
	  	        	imageViewAlarm.setVisibility(View.VISIBLE);
	  	        }
	        }
	    
		    
  		    return rowView;
	    
	  
	    // Bind the data efficiently with the holder.

	}
		

}
